package org.lbm.flow.agent;

import java.util.Map;
import java.util.HashMap;

/**
 * 智能体执行结果
 *
 * @author insist
 * @since 2025-08-01
 */
public class AgentResult {

    public enum Status {
        SUCCESS,    // 成功
        FAILED,     // 失败
        SKIPPED,    // 跳过
        TIMEOUT     // 超时
    }

    private final Status status;
    private final Object data;
    private final String message;
    private final Map<String, Object> metadata;
    private final long executionTime;

    private AgentResult(Status status, Object data, String message, Map<String, Object> metadata, long executionTime) {
        this.status = status;
        this.data = data;
        this.message = message;
        this.metadata = metadata != null ? new HashMap<>(metadata) : new HashMap<>();
        this.executionTime = executionTime;
    }

    /**
     * 创建成功结果
     */
    public static AgentResult success(Object data) {
        return new AgentResult(Status.SUCCESS, data, "执行成功", null, 0);
    }

    /**
     * 创建成功结果
     */
    public static AgentResult success(Object data, String message) {
        return new AgentResult(Status.SUCCESS, data, message, null, 0);
    }

    /**
     * 创建失败结果
     */
    public static AgentResult failed(String message) {
        return new AgentResult(Status.FAILED, null, message, null, 0);
    }

    /**
     * 创建失败结果
     */
    public static AgentResult failed(String message, Throwable cause) {
        return new AgentResult(Status.FAILED, cause, message, null, 0);
    }

    /**
     * 创建跳过结果
     */
    public static AgentResult skipped(String reason) {
        return new AgentResult(Status.SKIPPED, null, reason, null, 0);
    }

    /**
     * 创建超时结果
     */
    public static AgentResult timeout(String message) {
        return new AgentResult(Status.TIMEOUT, null, message, null, 0);
    }

    /**
     * 设置执行时间
     */
    public AgentResult withExecutionTime(long executionTime) {
        return new AgentResult(status, data, message, metadata, executionTime);
    }

    /**
     * 添加元数据
     */
    public AgentResult withMetadata(String key, Object value) {
        Map<String, Object> newMetadata = new HashMap<>(metadata);
        newMetadata.put(key, value);
        return new AgentResult(status, data, message, newMetadata, executionTime);
    }

    /**
     * 获取状态
     */
    public Status getStatus() {
        return status;
    }

    /**
     * 获取数据
     */
    @SuppressWarnings("unchecked")
    public <T> T getData() {
        return (T) data;
    }

    /**
     * 获取消息
     */
    public String getMessage() {
        return message;
    }

    /**
     * 获取元数据
     */
    public Map<String, Object> getMetadata() {
        return new HashMap<>(metadata);
    }

    /**
     * 获取执行时间
     */
    public long getExecutionTime() {
        return executionTime;
    }

    /**
     * 检查是否成功
     */
    public boolean isSuccess() {
        return status == Status.SUCCESS;
    }

    /**
     * 检查是否失败
     */
    public boolean isFailed() {
        return status == Status.FAILED;
    }

    /**
     * 检查是否跳过
     */
    public boolean isSkipped() {
        return status == Status.SKIPPED;
    }

    /**
     * 检查是否超时
     */
    public boolean isTimeout() {
        return status == Status.TIMEOUT;
    }

    @Override
    public String toString() {
        return String.format("AgentResult{status=%s, message='%s', executionTime=%dms}",
                           status, message, executionTime);
    }
}
